/*
 * Copyright (C) 2013 Michael
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package com.jm.jmsql.xport;

import com.jm.commons.logging.LogSystem;
import com.jm.commons.ui.UIBounds;
import com.jm.jmsql.models.XPortaTableModel;
import com.jm.xporta.XPortException;
import java.awt.Container;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

/**
 * 
 * @created Feb 15, 2013
 * @author Michael L.R. Marques
 */
public class XPortManager extends javax.swing.JDialog {
    
    /**
     * 
     */
    private ResultSet results;
    
    /** 
     * Creates new form XPortManager 
     * @param parent
     */
    public XPortManager(Container parent) {
        initComponents();
        UIBounds.setBounds(parent, this);
        // Set the icon image
        try {
            setIconImage(ImageIO.read(getClass().getClassLoader().getResource("com/jm/jmsql/icons/xport.png")));
        } catch (IOException ioe) {
            LogSystem.log(getClass(), getClass().getEnclosingMethod(), "Set image icon", ioe);
        }
        this.pnlBackground.setImage(new File("./images/optionslogo.png"));
        //
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    tableXportationOptions.setModel(new XPortaTableModel());
                    tableXportationOptions.getColumn("Description").setPreferredWidth((int) (tableXportationOptions.getWidth()*0.8));
                } catch (FileNotFoundException | InstantiationException | IllegalAccessException e) {
                    LogSystem.log(getClass(), getClass().getEnclosingMethod(), "Building the XPortaTableModel", e);
                }
            }
        }).start();
    }
    
    /**
     * 
     * @param results 
     */
    public void setResultSet(ResultSet results) {
        this.results = results;
    }
    
    /**
     * 
     * @param visible
     */
    @Override
    public void setVisible(boolean visible) {
        // Load the xports
        if (visible) {
            
        }
        //
        super.setVisible(visible);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlBackground = new com.jm.commons.components.panel.ImagePanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableXportationOptions = new javax.swing.JTable();
        btnDone = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("XPort Manager");
        setModal(true);
        setResizable(false);

        pnlBackground.setImage(null);

        tableXportationOptions.setAutoCreateRowSorter(true);
        tableXportationOptions.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tableXportationOptions.setRowHeight(20);
        tableXportationOptions.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                exportSelected(evt);
            }
        });
        jScrollPane1.setViewportView(tableXportationOptions);

        btnDone.setText("Done");
        btnDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                done(evt);
            }
        });

        javax.swing.GroupLayout pnlBackgroundLayout = new javax.swing.GroupLayout(pnlBackground);
        pnlBackground.setLayout(pnlBackgroundLayout);
        pnlBackgroundLayout.setHorizontalGroup(
            pnlBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 585, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBackgroundLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnDone)))
                .addContainerGap())
        );
        pnlBackgroundLayout.setVerticalGroup(
            pnlBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBackgroundLayout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDone)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlBackground, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlBackground, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void exportSelected(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_exportSelected
        try {
            if (evt.getClickCount() == 2 &&
                    evt.getButton() == java.awt.event.MouseEvent.BUTTON1 &&
                        this.tableXportationOptions.getRowCount() > 0 &&
                            this.tableXportationOptions.getSelectedRow() >= 0) {
                ((XPortaTableModel) this.tableXportationOptions.getModel()).getXPort(this.tableXportationOptions.getSelectedRow()).initialize(this.results);
                setVisible(false);
            }
        } catch (XPortException xpe) {
            LogSystem.log(getClass(), getClass().getEnclosingMethod(), "XPort option selected", xpe);
            JOptionPane.showMessageDialog(this, xpe.getMessage(), "XPort Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_exportSelected
    
    private void done(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_done
        try {
            if (this.tableXportationOptions.getRowCount() > 0 &&
                    this.tableXportationOptions.getSelectedRow() >= 0) {
                ((XPortaTableModel) this.tableXportationOptions.getModel()).getXPort(this.tableXportationOptions.getSelectedRow()).initialize(this.results);
                setVisible(false);
            }
        } catch (XPortException xpe) {
            LogSystem.log(getClass(), getClass().getEnclosingMethod(), "Initializing selected XPort", xpe);
            JOptionPane.showMessageDialog(this, xpe.getMessage(), "XPort Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_done
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDone;
    private javax.swing.JScrollPane jScrollPane1;
    private com.jm.commons.components.panel.ImagePanel pnlBackground;
    private javax.swing.JTable tableXportationOptions;
    // End of variables declaration//GEN-END:variables
    
}
